package com.comtop.util.report;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.poi.hwpf.HWPFDocument;

import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.w3c.dom.Document;

public class Converter {

    public static String start = "[-";
    public static String end = "-]";

    public Converter(File doc) {
        convert(doc);
    }

    private void convert(File docs) {
        try {
            FileInputStream finStream = new FileInputStream(docs.getAbsolutePath());
            HWPFDocument doc = new HWPFDocument(finStream);
            Document newDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(newDocument);

            wordToHtmlConverter.processDocument(doc);

            StringWriter stringWriter = new StringWriter();
            Transformer transformer = TransformerFactory.newInstance().newTransformer();

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            transformer.setOutputProperty(OutputKeys.METHOD, "html");
            transformer.transform(
                    new DOMSource(wordToHtmlConverter.getDocument()), new StreamResult(stringWriter));

            String html = stringWriter.toString();
            html = html.replaceAll("\\&lt;", "<");
            html = html.replaceAll("\\&rsquo;", "'");
            html = html.replaceAll("\\&rsquo;", "'");
            html = html.replaceAll("\\&gt;", ">");
            html = html.replaceAll("<br>", "</br>");
            html = clearCode(html);
//
//            if(html.contains(start)){
//                int iStart = html.indexOf(start);
//                int iEnd = html.indexOf(end);
//                String input = html.substring(iStart, iEnd+end.length());
//                html=html.replace(input, "<input />");
//                System.out.println(html.substring(iStart, iEnd+end.length()));
//            }
            File htmls = new File(docs.getAbsolutePath().replace(".doc", ".html"));
            if (htmls.exists()) {
                htmls.delete();
            } else {
                htmls.createNewFile();
            }

            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmls), "UTF-8"));
            out.write(html);
            out.close();
        } catch (Exception e) {
            System.err.println(e.getLocalizedMessage());
            e.printStackTrace();
        }
    }

    private String clearCode(String html) {
        if (html.contains("<META")) {
            int iStart = html.indexOf("<META");
            int iEnd = html.indexOf(">", iStart);
            String t=html.substring(iEnd, iEnd + 1);
            if (!"/".equals(html.substring(iEnd-1, iEnd))) {
                String input = html.substring(iStart, iEnd + 1);
                String temp = input.replace(">", "/>");
                html = html.replace(input, temp);
                if (html.contains("<META")) {
                    html = clearCode(html);
                }
            }
        }
        if (html.contains("<meta")) {
            int iStart = html.indexOf("<meta");
            int iEnd = html.indexOf(">", iStart);
            if (!"/".equals(html.substring(iEnd-1, iEnd))) {
                String input = html.substring(iStart, iEnd + 1);
                String temp = input.replace(">", "/>");
                html = html.replace(input, temp);
                if (html.contains("<meta")) {
                    html = clearCode(html);
                }
            }
        }
        return html;
    }
}
